home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
clang
/
xref.zip
/
XRF.C
< prev
next >
Wrap
Text File
|
1986-05-24
|
7KB
|
358 lines
/* XRF.C - 'C' and 'PASCAL' cross reference program.
from 6/86 Computer Language Magazine
Language: Microsoft C
Note:
Uses recursive functions, so when linking, you must include
the LINK switch '/SWITCH:48000'.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <io.h>
#include <malloc.h>
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define MAX_DEF 500 /* maximum language definitions */
#define NAME_CHARS "._" /* punctuation allowed in Names */
#define DQUOTE 0x22 /* ASCII Double Quote Character */
#define SQUOTE 0x27 /* ASCII Single Quote Character */
FILE *fopen(), *infile, *outfile;
char lbuff[120]; /* current line holder */
int lcnt = 1; /* line counter */
int lptr = 0; /* assume empty line buffer */
int is_c = FALSE; /* is input source in "C"? Allow { } */
struct numrec
{
int number; /* line number of symbol occurance */
struct numrec *nnext; /* next number of symbol line */
};
struct symrec
{
char *symbol; /* symbol string */
int count; /* symbol occurance counters */
struct numrec *lnum; /* line number records */
struct symrec *lnode; /* left branch of binary tree */
struct symrec *rnode; /* right branch of binary tree */
};
struct symrec *symtab = NULL; /* declare main symbol table */
char *Exceptions[ MAX_DEF ]; /* language definition table structure */
main(argc, argv)
char *argv[];
int argc;
{
int c;
if( (argc < 2) || (argc > 3) )
{
printf("XRF: Usage is XRF [input name] <output name>\n");
exit();
}
if(NULL==(infile = fopen(argv[1],"r")) )
{
printf("ERROR: Cannot open input file - %s\n",argv[1]);
exit();
}
if( argc == 3 )
{
if(NULL==(outfile = fopen( argv[2],"w")) )
{
printf("ERROR: Cannot open output file - %s\n",argv[2]);
exit();
}
printf("Generating Listing File - %s\n",argv[2]);
}
else
{
outfile = stdout;
}
load_tbl(argv[1]);
printf("Language Table Loaded\n");
/* Main input and parse loop */
while( (c=getc(infile)) != EOF ) filter (c);
fprintf( outfile, "Symbol Table for File - %s\n",strupr(argv[1]) );
symprint( symtab );
printf("\nEnd.\n");
}
/*
comment processor, quoted text, and punctuation
*/
filter( c )
char c;
{
char cl;
static is_com = FALSE;
if( c==DQUOTE)
{
while( (c=getc(infile)) != DQUOTE ) if( c==EOF ) return;
next_char( ' ' );
}
else
if( c==SQUOTE )
{
while((c=getc(infile)) != SQUOTE) if(c==EOF) return;
next_char(' ');
}
else
if((c=='{') && !is_c)
{
while((c=getc(infile)) != '}' ) if(c==EOF) return;
next_char( ' ' );
}
else
if(c == '(')
{
cl = getc(infile);
if (cl==EOF) return;
if(cl != '*')
{
next_char( ' ' );
ungetc(cl, infile);
}
else
{
is_com = TRUE;
while(is_com)
{
while((c=getc(infile)) != '*' ) if(c==EOF) return;
cl = getc(infile);
if (cl==EOF) return;
if(cl==')') is_com=FALSE;
else
ungetc(cl, infile);
}
next_char(' ');
}
}
else
if(c=='/')
{
cl=getc(infile);
if (cl==EOF) return;
if(cl != '*')
{
next_char(' ');
ungetc(cl, infile);
}
else
{
is_com = TRUE;
while(is_com)
{
while((c=getc(infile)) != '*') if(c=EOF) return;
cl = getc(infile);
if (cl==EOF) return;
if(cl=='/') is_com = FALSE;
else
ungetc(cl, infile);
}
next_char(' ');
}
}
else
{
if(isalnum(c) || strchr(NAME_CHARS, c) || isspace(c) )
next_char(c);
else
if(c=='-')
{
if('>' == (c=getc(infile)) )
{
next_char('-');
next_char('>');
}
else
{
ungetc(c, infile);
next_char(' ');
}
}
else
next_char(' ');
}
}
/* symbol process (after comments and quotes have been stripped) */
next_char(a)
char a;
{
if(isspace(a))
{
if(lptr==0)
{
if(a == '\n') ++lcnt;
return;
}
lbuff[lptr] = '\0';
lptr = 0;
next_sym( lbuff, lcnt);
if(a=='\n') ++lcnt;
}
else
lbuff[lptr++] = a;
}
next_sym(s, n)
char *s;
int n;
{
struct symrec *newsym();
if (isdigit(s[0])) return;
if (is_lang(s)) return;
symtab = newsym(symtab, s, n);
}
struct symrec *newsym(q, w, n)
struct symrec *q;
char *w;
int n;
{
struct symrec *salloc();
struct numrec *newnum();
char *strsave();
int cond;
if(q==NULL)
{
q = salloc();
strupr(w);
q->symbol = strsave(w);
q->count = 1;
q->lnum = q->lnode = q->rnode = NULL;
q->lnum = newnum(q->lnum, lcnt);
}
else
if((cond = strcmp(w, q->symbol)) == 0)
{
q->count++;
q->lnum = newnum( q->lnum, n);
}
else
if(cond<0)
q->lnode = newsym( q->lnode, w, n);
else
q->rnode = newsym( q->rnode, w, n);
return(q);
}
struct numrec *newnum(p, n)
struct numrec *p;
int n;
{
struct numrec *nalloc();
if(p==NULL)
{
p = nalloc();
p->number = n;
p->nnext = NULL;
}
else
{
p->nnext = newnum(p->nnext, n);
}
return(p);
}
struct symrec *salloc()
{
return((struct symrec *) malloc(sizeof(struct symrec)));
}
struct numrec *nalloc()
{
return((struct numrec *) malloc(sizeof(struct numrec)));
}
char *strsave(str)
char *str;
{
char *p;
if((p=malloc(strlen(str)+1)) != NULL) strcpy(p, str);
if(p==NULL) printf("ERROR: Out of string space.\n");
return(p);
}
symprint(p)
struct symrec *p;
{
if(p!=NULL)
{
symprint(p->lnode);
fprintf(outfile, "%-15s (%d)\t:\n",p->symbol, p->count);
numprint(p->lnum, 0);
symprint(p->rnode);
}
}
numprint(p, i)
struct numrec *p;
{
if(p != NULL)
{
fprintf(outfile, " %4d", p->number);
if(!((i+1)%10)) putc('\n', outfile);
numprint(p->nnext, ++i);
}
else
putc('\n', outfile);
}
is_lang(str)
char *str;
{
int i, j;
i = 0;
while(Exceptions[i] != NULL)
{
if((j=strcmp(strupr(str), Exceptions[i++])) == 0) return(1);
else
if(j<0) return(0);
}
return(0);
}
load_tbl(iname)
char *iname;
{
char *cptr;
char fname[65], symbol[500];
FILE *tfile;
int i = 0;
if(NULL == (cptr=strchr(iname, '.')))
{
printf("ERROR: No extension on input file name: %s\n",iname);
exit();
}
sscanf(++cptr, "%s", fname);
strcat(fname, ".XRF");
strupr(fname);
if(0 == strncmp(fname, "C.XRF", 5)) is_c = TRUE;
if(NULL == (tfile = fopen(fname,"r")))
{
printf("ERROR: Cannot open Language Table file: %s\n",fname);
exit();
}
while(i < MAX_DEF)
{
if(EOF == fscanf(tfile, "%s", symbol))
{
fclose(tfile);
return;
}
else
Exceptions[i++] = strsave(strupr(symbol));
}
printf("ERROR: Language Definition File is too long (Max = %d).\n",MAX_DEF);
exit();
}
_nullcheck()
{ }